var indexPa=1
var totalSum=0;
var productname="";
var addressCodess ="";
var starTime = "";
var endTiem = "";
var selectObj=[];
var selectAllId=[];
$(function () {
    "use strict";
    if(orgClass == "org_ebb_city"){
            fillCounty("520600000000","addressCode");
        }else{
            $("#hide-address").hide();
        }
      $("#addressCode").change(function(){
                    addressCodess=this.value;
                     loadProduct();
               });
    loadProduct();
    //导出
    $('#export').click(function () {
        exportProductInfo();
    });
});

//http://cloud.vimi8.top/industrial/product/allProductInfo?index=1&size=10&productName=yy&farmName=%E9%93%9C%E4%BB%81
function loadProduct() {
    $.ajax({
        type: "get",
        url: "/ebb/entity/admin/allproductinfo/?start="+indexPa+"&size=10&productName="+productname+"&addressCode="+addressCodess+"&startTime="+starTime+"&endTime="+endTiem,
       // data: JSON.stringify(params),
        contentType: "application/json",
        dataType:"json",
        success: function (result) {
            if (result.data.length > 0) {
                var totalCount = result.totalPages;
                totalPage = totalCount;
                $("#total-page").text(totalPage);

                $("#pagination-bar").show();
                $("#pagination-box").html('<ul id="pagination" class="pagination"></ul>');
                $('#pagination').twbsPagination({
                    totalPages: totalCount,
                    visiblePages: 5,
                    first: '第一页',
                    prev: '上一页',
                    next: '下一页',
                    last: '最后页',
                    onPageClick: function(event,page) {
                        indexPa=page;

                        $.ajax({
                                type: "get",
                                url: "/ebb/entity/admin/allproductinfo/?start="+indexPa+"&size=10&productName="+productname+"&addressCode="+addressCodess+"&startTime="+starTime+"&endTime="+endTiem,
                             //   data: JSON.stringify(params),
                              dataType:"json",
                                contentType: "application/json",
                                success: function (result) {
                                    totalSum = result.totalPages;
                                    var d = result.data;
                                    selectObj=d;
                                    $("input[name='checkboxAll']").prop("checked",false);
                                    selectAllId=[];
                                    //渲染数据
                                    $.templates("#product-list-tmpl").link("#product-list-data", d);
                                    totalPages = result.totalPages;
                                    $('#product-count').text(result.totalCount);
                                    $('#page-total').text(result.totalPages);
                                    $('#page-index').val(page);
                                },
                                error: function (XMLHttpRequest, textStatus, errorThrown) {
                                    if(XMLHttpRequest.status==401){
                                                                                    //跳转到登录页,带上登录后的跳转地址redirect_uri=???
                                                                                    // redirect_login();

                                                                                     ShowTip("你还没有登录，请先登录！", "success", 1000);
                                                                                                                            setTimeout(function () {
                                                                                                                                window.location = "login.html";
                                                                                                                            }, 1000);
                                                                                }
                                }
                            });
                    },
                    startPage: indexPa
                });
            } else {
                $("#product-list-data").html("<tr><td colspan='8'><h1 align='center' height='300px'>暂无数据</h1></td></tr>");
                $("#pagination-bar").hide();
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
               if(XMLHttpRequest.status==401){
                                 //跳转到登录页,带上登录后的跳转地址redirect_uri=???
                                 // redirect_login();
                  ShowTip("你还没有登录，请先登录！", "success", 1000);
                 setTimeout(function () {
                     window.location = "login.html";
                 }, 1000);
            }

         }
    });
}

function selectall(obj) {
    selectAllId=[];
    //获取checked属性
    if ($(obj).prop("checked")) {
        //设置checked属性
        $("input[name='checkboxAll']").prop("checked", true);
        $("input[name='check']").prop("checked", true);
        for(var i=0;i<selectObj.length;i++){
            selectAllId.push(selectObj[i].id);
        }
    } else {
        //设置checked属性
        $("input[name='checkboxAll']").prop("checked",false);
        $("input[name='check']").prop("checked", false);
    }
}

function selectsub(id) {
    //获取checked属性
    if ($("#"+id).prop("checked")) {
        //设置checked属性
        $("input[id=id]").prop("checked", true);
        var count=0;
        for(var i=0;i<selectAllId.length;i++){
            if(selectAllId[i]==id){
                count+=1;
            }
        }
        if(count==0){
            selectAllId.push(id);
        }
    } else {
        //设置checked属性
        $("input[id=id]").prop("checked",false);
        for(var i=0;i<selectAllId.length;i++){
            if(selectAllId[i]==id){
                selectAllId.splice(i,1);
            }
        }
    }
    if(selectAllId.length==selectObj.length){
        $("input[name='checkboxAll']").prop("checked", true);
    }else{
        $("input[name='checkboxAll']").prop("checked",false);
    }
}

function deleteSelectedAll() {
    if(selectAllId.length==0){
        ShowTip("请选择需要删除的项！", "danger", 1000);
        return;
    }
    var ids='';
    for(var i=0;i<selectAllId.length;i++){
        ids+="'"+selectAllId[i]+"'"+","
    }
    ids=ids.substring(0,ids.length-1);

    Showbox.Msg.confirm('您确定删除所选记录吗？',function(v){
        if(v=='yes'){
            $.ajax({
                type: "delete",
                url: "/ebb/entity/delSelectedAllProduct/",
                contentType: 'application/json',
                data: ids,
                success: function (data, status) {
                    if(data>0){
                        ShowTip("删除成功"+data+"条！", "success", 2000);
                        loadProduct();
                    }else{
                        ShowTip("删除失败！", "danger", 1000);
                    }
                },
                error:function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest);
                    ShowTip(XMLHttpRequest, "error", 1000);
                }
            });
        }
        if(v=='no'){
        }
    });
}

function comments() {
  productname= $("#productName").val().trim();
  if($("#platformDateStart").val().trim()!=""){
    starTime = $("#platformDateStart").val().trim();
  }
  if($("#platformDateEnd").val().trim()!=""){
    endTiem = $("#platformDateEnd").val().trim();
  }
  var addressCode1=$("#addressCode").val();
  if(addressCode1&&addressCode1.trim()!="--选择区域--"){
    addressCodess=$("#addressCode").val().trim();
  }
  indexPa = 1;
  loadProduct();
}




$('#go-index').click(function () {
    var index = $('#page-index').val();
    if(index>totalSum){
        index = totalSum;
    }
  indexPa=parseInt(index);
    loadProduct();
});



//删掉专产品http://cloud.vimi8.top/industrial/product/?id=old_5606f0798200756ca8cc621d
function deleteProduct(id){
if(orgClass!="org_ebb_entity"){
    ShowTip("你还没有权限做该操作！请联系管理员", "success", 1000);
     setTimeout(function () {
                                            window.location = "productManager.html";
                                        }, 1000);
return ;
}
     if(confirm("确定要删除该产品吗？")){
      $.ajax({
       type:"DELETE",
       url:"/ebb/entity/product/?id="+id,
        dataType: "json",
        success: function(data){
            if(!data.error){
              window.location.reload();
            }
        }
    });
   }
}

//导出产品信息
function exportProductInfo() {
      if($("#platformDateStart").val().trim()!=""){
        starTime = $("#platformDateStart").val().trim();
      }
      if($("#platformDateEnd").val().trim()!=""){
        endTiem = $("#platformDateEnd").val().trim();
      }
      var addressCode1=$("#addressCode").val();
      if(addressCode1&&addressCode1.trim()!="--选择区域--"){
        addressCodess=$("#addressCode").val().trim();
      }
    var xiProduct = $("#productName").val();
    window.location.href = "/ebb/entity/product/export?productName=" + xiProduct+"&addressCode="+addressCodess+"&startTime="+starTime+"&endTime="+endTiem;
}












